package com.dhcc.bpm.modules.metadata.service.impl;

import com.dhcc.bpm.modules.metadata.entity.ApiMetadata;
import com.dhcc.bpm.modules.metadata.vo.ApiMetadataVO;
import com.dhcc.bpm.modules.metadata.mapper.ApiMetadataMapper;
import com.dhcc.bpm.modules.metadata.service.IApiMetadataService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 接口定义数据表 服务实现类
 *
 * @author ccab
 * @since 2020-06-03
 */
@Service("opc_api_metadata")
public class ApiMetadataServiceImpl extends BaseServiceImpl<ApiMetadataMapper, ApiMetadata> implements IApiMetadataService {

	@Override
	public IPage<ApiMetadataVO> selectApiMetadataPage(IPage<ApiMetadataVO> page, ApiMetadataVO apiMetadata) {
		return page.setRecords(baseMapper.selectApiMetadataPage(page, apiMetadata));
	}

	@Override
	public List<ApiMetadataVO> selectApiMetadataList(ApiMetadataVO apiMetadata) {
		return  baseMapper.selectApiMetadataList(apiMetadata);
	}

	@Override
	public List<ApiMetadataVO> queryApiMetadataList(ApiMetadata apiMetadata) {
		return baseMapper.queryApiMetadataList(apiMetadata);
	}

	@Override
	public List<ApiMetadataVO> tree() {
		return ForestNodeMerger.merge(baseMapper.tree());
	}

	@Override
	public List<ApiMetadata> findByGroupId(Long groupId) {
		return baseMapper.findByGroupId(groupId);
	}

	@Override
	public List<ApiMetadata> findOtherByApiId(Long apiId) {
		return baseMapper.findOtherByApiId(apiId);
	}

	@Override
	public List<ApiMetadata> findReqObjectByApiId(Long apiId) {
		return baseMapper.findReqObjectByApiId(apiId);
	}

	@Override
	public List<ApiMetadata> findChildByApiId(Long apiId, Long groupId) {
		return baseMapper.findChildByApiId(apiId, groupId);
	}

	@Override
	public List<ApiMetadataVO> findChildByGroupId(Long groupId) {
		return baseMapper.findChildByGroupId(groupId);
	}

	@Override
	public ApiMetadata findParentObjectByApiId(Long apiId) {
		return baseMapper.findParentObjectByApiId(apiId);
	}

	@Override
	public ApiMetadata findResObjectByApiId(Long apiId) {
		return baseMapper.findResObjectByApiId(apiId);
	}

	@Override
	public List<String> getApiIds(int addType,Long baseProductId,Long opcApiIdReq, String inType,Integer bodyType,Long parentId,String apiComData) {
		if(parentId == null) {
			parentId = 0L;
		}
		return baseMapper.getApiIds(addType,baseProductId,opcApiIdReq, inType,bodyType,parentId,apiComData);
	}

	@Override
	public List<ApiMetadata> findOrderChild(ApiMetadata apiMetadata) {
		return baseMapper.findOrderChild(apiMetadata);
	}

	@Override
	public List<ApiMetadata> findOrderChild(int addType,Long baseProductId,Long apiId, String inType, int bodyType, Long groupId) {
		ApiMetadata apiMetadata = new ApiMetadata();
		if (addType==1){
			//系统
			apiMetadata.setSysId(apiId);
			if (baseProductId!=null&&baseProductId!=0){
				apiMetadata.setBaseProductId(baseProductId);
			}
		}else if (addType==2){
			//网关
			apiMetadata.setGatewayId(apiId);
		}else if (addType==3){
			//api
			apiMetadata.setApiId(apiId);
		}
		apiMetadata.setInType(inType);
		apiMetadata.setBodyType(bodyType);
		apiMetadata.setGroupId(groupId);
		return findOrderChild(apiMetadata);
	}

	@Override
	public Long findSysIdByApiId(Long apiId) {
		return baseMapper.findSysIdByApiId(apiId);
	}
}
